package com.zscat.mallplus.service.oms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zscat.mallplus.bill.entity.BillAftersales;
import com.zscat.mallplus.bill.entity.BillAftersalesItems;
import com.zscat.mallplus.bill.entity.BillAftersalesProgress;
import com.zscat.mallplus.bill.entity.BillAftersalesReason;
import com.zscat.mallplus.bill.mapper.BillAftersalesItemsMapper;
import com.zscat.mallplus.bill.mapper.BillAftersalesMapper;
import com.zscat.mallplus.bill.mapper.BillAftersalesProgressMapper;
import com.zscat.mallplus.bill.mapper.BillAftersalesReasonMapper;
import com.zscat.mallplus.entity.dto.ApplyAftersalesDto;
import com.zscat.mallplus.entity.vo.oms.*;
import com.zscat.mallplus.oms.entity.OmsOrder;
import com.zscat.mallplus.oms.entity.OmsOrderItem;
import com.zscat.mallplus.oms.mapper.OmsOrderItemMapper;
import com.zscat.mallplus.oms.mapper.OmsOrderMapper;
import com.zscat.mallplus.oms.vo.OmsOrderItmesVo;
import com.zscat.mallplus.pms.entity.PmsProduct;
import com.zscat.mallplus.pms.mapper.PmsProductMapper;
import com.zscat.mallplus.service.oms.service.IOmsAftersalesService;
import com.zscat.mallplus.vo.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author ZuRongTang
 * @Date 2020/3/5
 **/

@Service
public class OmsAftersalesServiceImpl extends ServiceImpl<BillAftersalesMapper, BillAftersales> implements IOmsAftersalesService {

    @Resource
    private OmsOrderMapper omsOrderMapper;

    @Resource
    private OmsOrderItemMapper omsOrderItemMapper;

    @Resource
    private PmsProductMapper pmsProductMapper;

    @Resource
    private BillAftersalesReasonMapper billAftersalesReasonMapper;

    @Resource
    private BillAftersalesMapper billAftersalesMapper;

    @Resource
    private BillAftersalesItemsMapper billAftersalesItemsMapper;

    @Resource
    private BillAftersalesProgressMapper billAftersalesProgressMapper;

    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    private final SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");


    @Override
    public PageInfo<AftersalesVo> getApplyAftersalesList(Long memberId, Integer pageIndex, Integer pageSize) {
        List<AftersalesVo> result = new ArrayList<>();

        Date date = new Date();

        List<OmsOrderItmesVo> applyAftersalesList = omsOrderMapper.getApplyAftersalesList(memberId, (pageIndex.longValue() - 1) * pageSize.longValue(), pageSize.longValue());


        if (applyAftersalesList.size() != 0) {
            for (OmsOrderItmesVo omsOrderItmesVo : applyAftersalesList) {
                AftersalesVo aftersalesVo = new AftersalesVo();
                Date receiveTime = omsOrderItmesVo.getReceiveTime();

                BeanUtils.copyProperties(omsOrderItmesVo, aftersalesVo);

                int daysBetween = getDaysBetween(date, receiveTime);

                if (daysBetween > 7) {
                    aftersalesVo.setAftersalesType(1);
                } else {
                    aftersalesVo.setAftersalesType(0);
                }
                result.add(aftersalesVo);
            }
        } else {
            return new PageInfo<>(pageIndex, pageSize, result);
        }

        return new PageInfo<>(pageIndex, pageSize, result);
    }

    @Override
    public PageInfo<AftersalesInfoVo> getAftersalesList(Long memberId, Integer pageIndex, Integer pageSize) {

        List<AftersalesInfoVo> result = new ArrayList<>();

        List<OmsOrderItmesVo> aftersalesList = omsOrderMapper.getAftersalesList(memberId, (pageIndex.longValue()-1)*pageSize, pageSize.longValue());

        if (aftersalesList.size() != 0) {

            for (OmsOrderItmesVo omsOrderItmesVo : aftersalesList) {

                AftersalesInfoVo aftersalesInfoVo = new AftersalesInfoVo();

                Long id = omsOrderItmesVo.getId();

                BillAftersalesItems billAftersalesItems = findBillAftresales("order_items_id", id);

                Long aftersalesId = billAftersalesItems.getAftersalesId();

                BillAftersales billAftersales = findBillAftersales("aftersales_id", aftersalesId);

                BeanUtils.copyProperties(omsOrderItmesVo, aftersalesInfoVo);

                aftersalesInfoVo.setAftersalesSn(billAftersales.getAftersalesSn());
                aftersalesInfoVo.setId(Long.valueOf(billAftersales.getAftersalesId()));
                aftersalesInfoVo.setStatus(omsOrderItmesVo.getAftersalesStatus());
                result.add(aftersalesInfoVo);

            }
        }

        return new PageInfo<>(pageIndex, pageSize, result);
    }

    @Override
    public AftersalesDetailsVo getAftersalesInfo(Long id) {

        AftersalesDetailsVo aftersalesDetailsVo = new AftersalesDetailsVo();

        BillAftersales billAftersales = findBillAftersales("aftersales_id", id);

        Integer type = billAftersales.getType();

        List<BillAftersalesProgress> progresses = findAftersaleState("aftersales_id", id);

        List<AftersaleStateVo> aftersaleStateVos = initState(type);

        if (type == 2) {
            for (int i = 0; i < progresses.size(); i++) {

                AftersaleStateVo aftersaleStateVo = aftersaleStateVos.get(i);

                if (i == 1) {
                    BillAftersalesProgress billAftersalesProgress = progresses.get(i);
                    Integer state = billAftersalesProgress.getAftersalesStatus();
                    if (state == 1) {
                        aftersaleStateVo.setHint("申请被拒");
                        aftersaleStateVo.setState(0);
                    }
                }
                aftersaleStateVo.setState(progresses.get(i).getState());

                String format = simpleDateFormat1.format(new Date(progresses.get(i).getCreateTime()));
                aftersaleStateVo.setCreateTime(format);
            }


        } else if (type == 3) {
            for (int i = 0; i < progresses.size(); i++) {

                AftersaleStateVo aftersaleStateVo = aftersaleStateVos.get(i);

                if (i == 1) {
                    BillAftersalesProgress billAftersalesProgress = progresses.get(i);
                    Integer state = billAftersalesProgress.getAftersalesStatus();
                    if (state == 1) {
                        aftersaleStateVo.setHint("申请被拒");
                        aftersaleStateVo.setState(0);
                    }
                }
                aftersaleStateVo.setState(progresses.get(i).getState());

                String format = simpleDateFormat1.format(new Date(progresses.get(i).getCreateTime()));
                aftersaleStateVo.setCreateTime(format);
            }
        }else {
            for (int i = 0; i < progresses.size(); i++) {

                AftersaleStateVo aftersaleStateVo = aftersaleStateVos.get(i);

                if (i == 1) {
                    BillAftersalesProgress billAftersalesProgress = progresses.get(i);
                    Integer state = billAftersalesProgress.getAftersalesStatus();
                    if (state == 1) {
                        aftersaleStateVo.setHint("申请被拒");
                        aftersaleStateVo.setState(0);
                    }
                }
                aftersaleStateVo.setState(progresses.get(i).getState());

                String format = simpleDateFormat1.format(new Date(progresses.get(i).getCreateTime()));
                aftersaleStateVo.setCreateTime(format);
            }
        }

        Date ctime = billAftersales.getCtime();
        String format = simpleDateFormat.format(ctime);


        aftersalesDetailsVo.setCtime(format);
        aftersalesDetailsVo.setMark(billAftersales.getMark());
        aftersalesDetailsVo.setDescription(billAftersales.getDescription());
        aftersalesDetailsVo.setAftersalesSn(billAftersales.getAftersalesSn());
        aftersalesDetailsVo.setStart(aftersaleStateVos);

        return aftersalesDetailsVo;
    }

    @Override
    public ApplyAftersalesInfoVo ApplyAftersalesInfo(Long id) {
        ApplyAftersalesInfoVo applyAftersalesInfoVo = new ApplyAftersalesInfoVo();


        OmsOrderItem omsOrderItem = omsOrderItemMapper.selectById(id);

        BeanUtils.copyProperties(omsOrderItem, applyAftersalesInfoVo);

        Integer storeId = omsOrderItem.getStoreId();

        List<BillAftersalesReason> aftersalesReason = findAftersalesReason("store_id", storeId, "status", 1);

        applyAftersalesInfoVo.setAftersalesReason(aftersalesReason);

        return applyAftersalesInfoVo;
    }

    @Override
    public void commiterAftersales(Long memberId, ApplyAftersalesDto applyAftersalesDto) {

        String aftersalesSn = null;

        Long id = applyAftersalesDto.getId();

        OmsOrderItem omsOrderItem = omsOrderItemMapper.selectById(id);

        OmsOrder omsOrder = omsOrderMapper.selectById(omsOrderItem.getOrderId());


        omsOrderItem.setAftersalesStatus(1);

        omsOrderItemMapper.updateById(omsOrderItem);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        List<BillAftersales> ctime = findAftersales("ctime", new Date());

        if (ctime.size() == 0) {
            aftersalesSn = year + "" + month + "" + day + "01" + "03" + "100001";
        } else {
            BillAftersales billAftersales = ctime.get(0);

            String aftersalesSn1 = billAftersales.getAftersalesSn();
            Long aLong = Long.valueOf(aftersalesSn1);
            aLong = aLong + 1;
            aftersalesSn = aLong.toString();
        }

        BillAftersales billAftersales = new BillAftersales();

        billAftersales.setUserId(memberId);
        billAftersales.setOrderId(omsOrderItem.getOrderId().toString());
        billAftersales.setRefund(omsOrderItem.getRealAmount());
        billAftersales.setReason(applyAftersalesDto.getReason());
        billAftersales.setCtime(new Date());
        billAftersales.setUtime(new Date());
        billAftersales.setStoreId(omsOrderItem.getStoreId());
        billAftersales.setDescription(applyAftersalesDto.getDescription());
        billAftersales.setProofPics(applyAftersalesDto.getProofPics());
        billAftersales.setLinkMan(omsOrder.getReceiverName());
        billAftersales.setLinkPhone(omsOrder.getReceiverPhone());
        billAftersales.setAddress(omsOrder.getReceiverDetailAddress());
        billAftersales.setAftersalesSn(aftersalesSn);
        billAftersales.setAftersalesStatus(0);
        billAftersales.setRefundStatus(0);
        billAftersales.setStatus(0);
        billAftersales.setAftersalesContent("");

        Integer insert = billAftersalesMapper.insert(billAftersales);

        BillAftersalesItems billAftersalesItems = new BillAftersalesItems();

        billAftersalesItems.setAftersalesId(billAftersales.getAftersalesId());
        billAftersalesItems.setOrderItemsId(id);
        billAftersalesItems.setSkuId(omsOrderItem.getProductSkuId());
        billAftersalesItems.setProductId(omsOrderItem.getProductId());
        billAftersalesItems.setName(omsOrderItem.getProductName());
        billAftersalesItems.setImageUrl(omsOrderItem.getProductPic());
        billAftersalesItems.setNums(omsOrderItem.getProductQuantity());
        billAftersalesItems.setCtime(new Date());
        billAftersalesItems.setUtime(new Date());
        billAftersalesItems.setStoreId(omsOrderItem.getStoreId());
        billAftersalesItems.setStatus(1);
        billAftersalesItems.setProductPrice(omsOrderItem.getProductPrice());
        billAftersalesItems.setProductRealPrice(omsOrderItem.getRealAmount());
        billAftersalesItems.setProductAttr(omsOrderItem.getProductAttr());

        billAftersalesItemsMapper.insert(billAftersalesItems);


        BillAftersalesProgress billAftersalesProgress = new BillAftersalesProgress();
        billAftersalesProgress.setAftersalesId(billAftersales.getAftersalesId());
        billAftersalesProgress.setCreateTime(System.currentTimeMillis());
        billAftersalesProgress.setUpdateTime(System.currentTimeMillis());
        billAftersalesProgress.setState(0);
        billAftersalesProgress.setAftersalesStatus(0);

        billAftersalesProgressMapper.insert(billAftersalesProgress);


    }

    private List<BillAftersales> findAftersales(String column, Object value) {
        QueryWrapper<BillAftersales> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column, value).orderByAsc("aftersales_id");
        return billAftersalesMapper.selectList(queryWrapper);
    }

    private List<BillAftersalesReason> findAftersalesReason(String column, Object value, String column1, Object value1) {
        QueryWrapper<BillAftersalesReason> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column, value).eq(column1, value1);
        return billAftersalesReasonMapper.selectList(queryWrapper);
    }

    public List<AftersaleStateVo> initState(Integer type) {

        AftersaleStateVo aftersaleStateVo = new AftersaleStateVo();
        AftersaleStateVo aftersaleStateVo1 = new AftersaleStateVo();
        AftersaleStateVo aftersaleStateVo2 = new AftersaleStateVo();
        AftersaleStateVo aftersaleStateVo3 = new AftersaleStateVo();
        AftersaleStateVo aftersaleStateVo4 = new AftersaleStateVo();

        List<AftersaleStateVo> result = new ArrayList<>();

        if (type == 2) {


            aftersaleStateVo.setState(1);
            aftersaleStateVo.setHint("发起申请");

            aftersaleStateVo1.setState(1);
            aftersaleStateVo1.setHint("申请通过");

            aftersaleStateVo2.setState(1);
            aftersaleStateVo2.setHint("售后收货");

            aftersaleStateVo3.setState(1);
            aftersaleStateVo3.setHint("进行退款");

            aftersaleStateVo4.setState(1);
            aftersaleStateVo4.setHint("处理完成");

            result.add(aftersaleStateVo);
            result.add(aftersaleStateVo1);
            result.add(aftersaleStateVo2);
            result.add(aftersaleStateVo3);
            result.add(aftersaleStateVo4);


        } else {

            aftersaleStateVo.setState(1);
            aftersaleStateVo.setHint("发起申请");

            aftersaleStateVo1.setState(1);
            aftersaleStateVo1.setHint("申请通过");

            aftersaleStateVo2.setState(1);
            aftersaleStateVo2.setHint("售后收货");

            aftersaleStateVo3.setState(1);
            aftersaleStateVo3.setHint("进行换货");

            aftersaleStateVo4.setState(1);
            aftersaleStateVo4.setHint("处理完成");

            result.add(aftersaleStateVo);
            result.add(aftersaleStateVo1);
            result.add(aftersaleStateVo2);
            result.add(aftersaleStateVo3);
            result.add(aftersaleStateVo4);

        }

        return result;
    }


    private List<BillAftersalesProgress> findAftersaleState(String column, Object value) {
        QueryWrapper<BillAftersalesProgress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column, value).orderByAsc("create_time");
        return billAftersalesProgressMapper.selectList(queryWrapper);
    }


    private BillAftersales findBillAftersales(String column, Object value) {
        QueryWrapper<BillAftersales> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column, value);
        return billAftersalesMapper.selectOne(queryWrapper);
    }

    private BillAftersalesItems findBillAftresales(String column, Object value) {
        QueryWrapper<BillAftersalesItems> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column, value);
        return billAftersalesItemsMapper.selectOne(queryWrapper);
    }


    public int getDaysBetween(Date dtBegin, Date dtEnd) {
        return new Long((dtBegin.getTime() - dtEnd.getTime()) / 86400000).intValue();
    }
}
